//
//  AppDelegate.m
//  ShoppingMall
//
//  Created by Demon on 2018/3/21.
//  Copyright © 2018年 Demon. All rights reserved.
//

#import "AppDelegate.h"
#import "XGPush.h"
#import "SMMainTabbarController.h"
#import "SMBaseNavigationController.h"
#import "YDWebViewController.h"

@interface AppDelegate () <XGPushDelegate>

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [[XGPush defaultManager] startXGWithAppID:2200280036 appKey:@"I1R97X9GFD3L" delegate:self];
    [[XGPush defaultManager] reportXGNotificationInfo:launchOptions];
    return YES;
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    [[XGPush defaultManager] setXgApplicationBadgeNumber:0];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    NSLog(@"[XGDemo] receive Notification\n%@",userInfo);
    [[XGPush defaultManager] reportXGNotificationInfo:userInfo];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    NSLog(@"[XGDemo] userinfo %@", userInfo);
    [[XGPush defaultManager] reportXGNotificationInfo:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
}

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
- (void)xgPushUserNotificationCenter:(UNUserNotificationCenter *)center
      didReceiveNotificationResponse:(UNNotificationResponse *)response
               withCompletionHandler:(void(^)(void))completionHandler  API_AVAILABLE(ios(10.0)) API_AVAILABLE(ios(10.0)){
    NSLog(@"xgPushUserNotificationCenter-后台进入--  %@",[response.notification.request.content.userInfo jsonPrettyStringEncoded]);
    [[XGPush defaultManager] reportXGNotificationInfo:response.notification.request.content.userInfo];
    completionHandler();
    
    NSDictionary *aps = [response.notification.request.content.userInfo objectForKey:@"aps"];
    NSDictionary *alert = aps[@"alert"];
    NSString *url = alert[@"body"];
    
    SMMainTabbarController *mainVC = (SMMainTabbarController *)self.window.rootViewController;
    UIViewController *vc = [[(SMBaseNavigationController *)mainVC.selectedViewController viewControllers] objectAtIndex:0];
    YDWebViewController *webVC = ALLOC(YDWebViewController);
    webVC.webUrl = url;
    [vc.navigationController pushViewController:webVC animated:YES];
}

- (void)xgPushUserNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler  API_AVAILABLE(ios(10.0)) API_AVAILABLE(ios(10.0)) API_AVAILABLE(ios(10.0)){
    NSLog(@"xgPushUserNotificationCenter++前台接受++  %@",[notification.request.content.userInfo jsonPrettyStringEncoded]);
    [[XGPush defaultManager] reportXGNotificationInfo:notification.request.content.userInfo];
    completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
}
#endif

@end

